/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package UserInterface;

import Controller.SRCFacade;
import Exceptions.ConfigFileNotLoadedException;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;

/**
 *
 * @author subx
 */
class LoadCommand extends Command {

    public LoadCommand() {
        
    }

    @Override
    public void execute(ActionEvent evt) {
        SRCFacade facade = SRCFacade.getInstance();


        try {
            int result;
            File file;
            JFileChooser chooser = new JFileChooser();

            //Exibe JFileChooser e recolhe as informações
            result = chooser.showOpenDialog(SRCMain.getInstance());

            if(result == JFileChooser.APPROVE_OPTION){
                String[] temp = null;
                SRCMain main = SRCMain.getInstance();
                file = chooser.getSelectedFile();
                
                //Solicita a fachada que carregue o arquivo de configuração especificado
                facade.loadConfig(file);
                
                //Atualiza as informações do painel de informações. ¬¬
                temp = facade.getInfos();
                main.setInfos(temp);
                
            }
            
        } catch (FileNotFoundException ex) {
            AlertWindow alert = new AlertWindow();

            alert.showAlertWindow(SRCMain.getInstance(),
                                  "Carregar configuração.",
                                  ex);
        }catch(ConfigFileNotLoadedException ex){
            AlertWindow alert = new AlertWindow();

            alert.showAlertWindow(SRCMain.getInstance(),
                                  "Configurações não foram carregadas.",
                                  ex);
        }

    }

    /*private void test(String map) {
        JFrame frame = new JFrame("teste");
        MapCanvas canv = SRCMain.getInstance().getCanvas();

        canv.setBounds(new Rectangle(100,100));
        frame.setBounds(new Rectangle(500,500));
        frame.add(canv);
        frame.setVisible(true);

    }*/

}
